/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrador
 */
public class Client {
    
    private String address;
    private int port;
    Socket socket = null;

    public Client(Socket socket){
        this.socket = socket;
    	this.address = socket.getRemoteSocketAddress().toString();
        this.port = socket.getPort();
    
    }

    
    public Client(String address, int port){
        this.address = address;
        this.port = port;
    }

    public void close() {
        try {
            socket.close();
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
        socket = null;
    }
    
    public String sendMessage(String message) throws IOException{
        if(socket==null) socket = new Socket(address, port);      
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);                                
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out.println(message);    
        String response;        
        while((response = in.readLine())==null){}                              
        return response;
    }
    
    public void sendNoResponseMessage(String message) throws IOException{
        if(socket==null) socket = new Socket(address, port);      
        PrintWriter out = new PrintWriter(socket.getOutputStream(), true);                                
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        out.println(message);    
    }    
    
}
